﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Web.Model;
using Portal.Cms.Model;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Core;

namespace Portal.Cms.Render
{
    public class ContactUsRenderBll
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html =
                   " <script language=javascript>\r\n" +
                   "    function ClientDelete(ContactUsId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.ContactUsRenderBll.ServerSideDelete(SiteId,ContactUsId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +

                   "    function ClientView(ContactUsId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Cms.Render.ContactUsRenderBll.DrawViewForm(SiteId, ContactUsId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divActionForm').style.display='block';\r\n"+
                   "    }\r\n" +

                   "    function ClientBack()\r\n"+
                   "    {\r\n"+
                   "        document.getElementById('divActionForm').style.display='none';\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n"+
                   " </script>\r\n" +
                   "<div id=divActionForm class=FormSaveUpdate></div>\r\n" +
                   "<div id=divListForm class=tabcontent>\r\n" +
                        ServerSideReading(SiteId).HtmlContent +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawViewForm(string SiteId, string ContactUsId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                ContactUsCls OContactUs = ContactUsProcessBll.CreateContactUsModel(SiteId, ContactUsId);
                string Html =
                   "  <h3>Xem thông tin liên hệ</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Vị trí:</td>\r\n" +
                   "        <td class=c2>" + OContactUs.Title + "</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Người gửi:</td>\r\n" +
                   "        <td class=c2>" + OContactUs.FullName + "</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Địa chỉ:</td>\r\n" +
                   "        <td class=c2>" + OContactUs.Address + "</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Điện thoại:</td>\r\n" +
                   "        <td class=c2>" + OContactUs.PhoneNumber + "</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Email:</td>\r\n" +
                   "        <td class=c2>" + OContactUs.Email + "</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Nội dung:</td>\r\n" +
                   "        <td class=c2>" + OContactUs.Body + "</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Gửi lúc:</td>\r\n" +
                   "        <td class=c2>" + OContactUs.EntryDate.ToString("dd/MM/yyyy HH:mm") + "</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1></td>\r\n" +
                   "        <td class=c2><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                ContactUsCls[] ContactUs = ContactUsProcessBll.ReadingContactUs(SiteId);
                string Html = "";
                if (ContactUs.Length == 0)
                {
                    Html = "<h3>Không có liên hệ nào</h3>";
                }
                else
                {
                    Html =
                    "        <h3>Contact Us<input type=button class=button value=\"Quay lại\" onclick=\"javascript:window.open('home.aspx','_self');\" style=\"float:right;margin-right:10px;\"></h3>\r\n" +
                    "            <table>\r\n" +
                    "                <tr>\r\n" +
                    "                    <th>#</th>\r\n" +
                    "                    <th>Người gửi</th>\r\n" +
                    "                    <th>Địa chỉ</th>\r\n" +
                    "                    <th>Email</th>\r\n" +
                    "                    <th>Số phone</th>\r\n" +
                    "                    <th>Nội dung</th>\r\n" +
                    "                    <th></th>\r\n" +
                    "                    <th></th>\r\n" +
                    "                </tr>\r\n";
                    for (int iIndex = 0; iIndex < ContactUs.Length; iIndex++)
                    {
                        Html +=
                           "                <tr>\r\n" +
                           "                    <td style=\"width:30px;vertical-align:right;\">" + (iIndex + 1) + "</td>\r\n" +
                           "                    <td>" + ContactUs[iIndex].FullName + "</td>\r\n" +
                           "                    <td>" + ContactUs[iIndex].Address + "</td>\r\n" +
                           "                    <td>" + ContactUs[iIndex].Email + "</td>\r\n" +
                           "                    <td>" + ContactUs[iIndex].PhoneNumber + "</td>\r\n" +
                           "                    <td>" + ContactUs[iIndex].Body + "</td>\r\n" +
                           "                    <td style=\"width:10px;\"><input type=button class=button value=\"Xem\" onclick=\"javascript:ClientView('" + ContactUs[iIndex].ContactUsId + "');\"></td>\r\n" +
                           "                    <td style=\"width:10px;\"><input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDelete('" + ContactUs[iIndex].ContactUsId + "');\"></td>\r\n" +
                           "                </tr>\r\n";
                    }
                    Html +=
                       "    </table>\r\n";
                }
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }

        
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDelete(string SiteId, string ContactUsId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                ContactUsProcessBll.DeleteContactUs(SiteId, ContactUsId);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }
    }
}
